---
title: "Already Gone_Code"
authors: Noah Haynes & Jordan Butcher
output: html_document
modified for LSQ Replication: "2023-07-31"
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

library(ggplot2)
library(tidyverse)
library(dplyr)
library(patchwork)
library(ggrepel)
library(viridis)
library(hrbrthemes)
library(ggthemes)
library(maps)
library(tibbletime)
library(statebins)
library(RColorBrewer)
library(usmap)
library(scales)
library(haven)
```


```{r}
fem <- read_dta("Womens Turnover Dataset_foranalysis.dta")
prfem <- group_by(fem, state) %>% summarize(pr = mean(pr_femseat))
a <- statebins(state_data = prfem,          
          state_col = "state", value_col = "pr", dark_label = "White", light_label = "White", font_size=5) + 
            scale_fill_gradient2(low = "white",
                                high = "black", name = "Women in State Legislatures", limits=c(0,40),
                                breaks = c(0,10,20,30,40), labels = c("0%","10%","20%","30%","40%" )) + theme_statebins()

a
```

```{r}
femc<- group_by(fem, state) %>% summarize(caucus = mean(fem_caucus))
b <- statebins(state_data = femc,          
          state_col = "state", value_col = "caucus", dark_label = "White", light_label = "White", font_size=5) + 
            scale_fill_gradient2(low = "white",
                                 mid = "grey",
                                high = "black", name = "Women's Caucus", limits=c(-1,1),
                                breaks = c(-1,0,1), labels = c("","No","Yes")) + theme_statebins()
b
```

```{r}
femturn<- group_by(fem, state) %>% summarize(turn = mean(femturn_tot))
c <- statebins(state_data = femturn,          
          state_col = "state", value_col = "turn", dark_label = "White", light_label = "White", font_size=5) + 
            scale_fill_gradient2(low = "white",
                                high = "black", name = "Women's Turnover", limits=c(0,6),
                                breaks = c(0,3,6), labels = c("0%","3%","6%")) + theme_statebins()
c
```

```{r}
ggsave("fem_seats_bins.jpeg", plot = a, scale = 1, width = 20, height = 10, units = c("in"), dpi = 500)
ggsave("FemaleCaucus_greyscale_statebins.jpeg", plot = b, scale = 1, width = 20, height = 10, units = c("in"), dpi = 500)
ggsave("fem_turn_bins.jpeg", plot = c, scale = 1, width = 20, height = 10, units = c("in"), dpi = 500)
```

```{r}
library(forcats)
library(haven)
library(ggplot2)
library(tidyverse)
library(dplyr)
library(patchwork)
library(ggrepel)
library(viridis)
library(hrbrthemes)
library(ggthemes)
sub.eff <- read_csv("female_probs.csv")
sub.eff$Chamber <- factor(sub.eff$Chamber,          
                         levels = c("upper", "lower"))

```


```{r}
ggplot(sub.eff, aes(x = x, y = mean)) + 
  geom_pointrange(mapping = aes(ymin = low, ymax = high, shape = Chamber)) + 
  scale_x_continuous(breaks = c(1.5,2.5,3.5,4.5,5.5,6.5,7.5), labels = c("Salary", "Staff", "Woman Protem", "Woman Speaker/President", "Bipartisan Caucus", "Uncontested Seats", "Days in Session")) + 
  geom_hline(yintercept = 0, linetype = "dotted") +
  labs(x = "", y = expression(Delta~"Pr(Turnover)")) +
  coord_flip() + theme_bw() + 
  theme(axis.text = element_text(color = "black"))
ggsave("Female Probabilities.jpg", width = 10, height = 8, dpi = 1000)
```

```{r}
sub.eff1 <- read_csv("female_probs_term.csv")
sub.eff1$Chamber <- factor(sub.eff1$Chamber,                 # Relevel group factor
                         levels = c("upper", "lower"))

ggplot(sub.eff1, aes(x = x, y = mean)) + 
  geom_pointrange(mapping = aes(ymin = low, ymax = high, shape = Chamber)) + 
  scale_x_continuous(breaks = c(1.5,2.5,3.5,4.5,5.5,6.5,7.5), labels = c("Salary", "Staff", "Woman Protem", "Woman Speaker/President", "Bipartisan Caucus", "Uncontested Seats", "Days in Session")) +
  geom_hline(yintercept = 0, linetype = "dotted") +
  labs(x = "", y = expression(Delta~"Pr(Turnover)")) +
  coord_flip() + theme_bw() +
  theme(axis.text = element_text(color = "black"))
ggsave("Female Probabilities_Term.jpg", width = 10, height = 8, dpi = 1000)
```
```{r}
sub.eff1 <- read_csv("fem_prob_all.csv")
sub.eff1$Chamber <- factor(sub.eff1$Chamber,                 # Relevel group factor
                         levels = c("upper", "lower"))

ggplot(sub.eff1, aes(x = x, y = mean)) + 
  geom_pointrange(mapping = aes(ymin = low, ymax = high, shape = Chamber, linetype = Type)) + 
  scale_x_continuous(breaks = c(1,5,9,13,17,21,25), labels = c("Days in Session", "Uncontested Seats", "Bipartisan Caucus", "Woman Speaker/President", "Woman Protem", "Staff", "Salary")) +
  geom_hline(yintercept = 0, linetype = "dotted") +
  labs(x = "", y = expression(Delta~"Pr(Turnover)")) +
  coord_flip() + theme_bw() +
  theme(axis.text = element_text(color = "black"))
  
ggsave("Female Probabilities_all.jpg", width = 10, height = 8, dpi = 1000)
```